//
//  NSString+XXSecurity.m
//  SafetyFireControlSystem
//
//  Created by zhaonaixin on 2021/5/18.
//  Copyright © 2021 小新. All rights reserved.
//

#import "NSString+XXSecurity.h"
#import <CommonCrypto/CommonCryptor.h>

@implementation NSString (XXSecurity)

- (NSString *)aes256_encrypt:(NSString *)key IV:(NSString *)IV
{    
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    
//    char keyPtr[kCCKeySizeAES128+1];
//    bzero(keyPtr, sizeof(keyPtr));
//    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
//
//    char ivPtr[kCCKeySizeAES128+1];
//    memset(ivPtr, 0, sizeof(ivPtr));
//    [IV getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
//
//    NSUInteger dataLength = [data length];
//    size_t bufferSize = dataLength + kCCBlockSizeAES128;
//    void *buffer = malloc(bufferSize);
//    size_t numBytesEncrypted = 0;
//    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
//                                          kCCAlgorithmAES,
//                                          kCCOptionPKCS7Padding,
//                                          keyPtr,
//                                          kCCKeySizeAES128,
//                                          ivPtr,
//                                          [data bytes],
//                                          dataLength,
//                                          buffer,
//                                          bufferSize,
//                                          &numBytesEncrypted);
//    if (cryptStatus == kCCSuccess) {
//
//        NSData *result = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
//
//        //base64
//        return [result base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//    }
//    free(buffer);
//    return nil;
    
    NSData *result = [self data:data AES128operation:kCCEncrypt key:key iv:IV];
    
    //base64
    return [result base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

- (NSData *)data:(NSData *)data AES128operation:(CCOperation)operation key:(NSString *)key iv:(NSString *)iv
{
    char keyPtr[kCCKeySizeAES128 + 1];
    bzero(keyPtr, sizeof(keyPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    // IV
    char ivPtr[kCCBlockSizeAES128 + 1];
    bzero(ivPtr, sizeof(ivPtr));
    [iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
    
    size_t bufferSize = [data length] + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesEncrypted = 0;
    
    
    CCCryptorStatus cryptorStatus = CCCrypt(operation, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                            keyPtr, kCCKeySizeAES128,
                                            ivPtr,
                                            [data bytes], [data length],
                                            buffer, bufferSize,
                                            &numBytesEncrypted);
    
    if(cryptorStatus == kCCSuccess){
        NSLog(@"Success");
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }else{
        NSLog(@"Error");
    }
    
    free(buffer);
    return nil;
}

@end
